import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import java.awt.MouseInfo;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.event.MouseInputListener;


public class P04_Panel extends JPanel  implements MouseListener, MouseMotionListener, ActionListener {
	
	private PanelListener listener;
	
	//1920
	private int width=0;
	//1080
	private int height=0;
	
	//menu control
	private Image[] imageMenu = new Image[9];
	private boolean[] menu_presseed = new boolean[4];
	private boolean help_pressed = false;
	
	private Image[] imageList = new Image[7];
	
	private String[] left_list = new String[14];
	
	private String[] right_list = new String[200];
	
	public int man=0;
	
	private int left_position=0;
	private int right_position=0;
	
	private boolean moving=false;
	
	private boolean[] buttons_pressed = new boolean[4];
	private int buttons_position=-1;
	
	private final int GAP =69;
	
	private Timer timer;
	private boolean long_pressed=false;
	
	public P04_Panel(int width, int height){
		
		this.width=width;
		this.height=height;
		
		//menu control
		imageMenu[0]=new ImageIcon(getClass().getResource("images/m/h0.png")).getImage();
		imageMenu[1]=new ImageIcon(getClass().getResource("images/m/h1.png")).getImage();
		imageMenu[2]=new ImageIcon(getClass().getResource("images/m/f0.png")).getImage();
		imageMenu[3]=new ImageIcon(getClass().getResource("images/m/f1.png")).getImage();
		imageMenu[4]=new ImageIcon(getClass().getResource("images/m/b0.png")).getImage();
		imageMenu[5]=new ImageIcon(getClass().getResource("images/m/b1.png")).getImage();
		imageMenu[6]=new ImageIcon(getClass().getResource("images/m/q0.png")).getImage();
		imageMenu[7]=new ImageIcon(getClass().getResource("images/m/q1.png")).getImage();
		imageMenu[8]=new ImageIcon(getClass().getResource("images/m/help/04.png")).getImage();
		
		menu_presseed[0]=false;
		menu_presseed[1]=false;
		menu_presseed[2]=false;
		menu_presseed[3]=false;
		
		
		imageList[0]=new ImageIcon(getClass().getResource("images/4/background.png")).getImage();
		imageList[1]=new ImageIcon(getClass().getResource("images/4/left_table.png")).getImage();
		imageList[2]=new ImageIcon(getClass().getResource("images/4/right_table.png")).getImage();
		imageList[3]=new ImageIcon(getClass().getResource("images/4/up00.png")).getImage();
		imageList[4]=new ImageIcon(getClass().getResource("images/4/up01.png")).getImage();
		imageList[5]=new ImageIcon(getClass().getResource("images/4/down00.png")).getImage();
		imageList[6]=new ImageIcon(getClass().getResource("images/4/down01.png")).getImage();
		
		left_list[0]="images/4/left/01.png";
		left_list[1]="images/4/left/02.png";
		left_list[2]="images/4/left/03.png";
		left_list[3]="images/4/left/04.png";
		left_list[4]="images/4/left/05.png";
		left_list[5]="images/4/left/06.png";
		left_list[6]="images/4/left/07.png";
		left_list[7]="images/4/left/08.png";
		left_list[8]="images/4/left/09.png";
		left_list[9]="images/4/left/10.png";
		left_list[10]="images/4/left/11.png";
		left_list[11]="images/4/left/12.png";
		left_list[12]="images/4/left/13.png";
		left_list[13]="images/4/left/14.png";
		
		right_list[0]="images/4/right/1.png";
		right_list[1]="images/4/right/2.png";
		right_list[2]="images/4/right/3.png";
		right_list[3]="images/4/right/4.png";
		right_list[4]="images/4/right/5.png";
		right_list[5]="images/4/right/6.png";
		right_list[6]="images/4/right/7.png";
		right_list[7]="images/4/right/8.png";
		right_list[8]="images/4/right/9.png";
		right_list[9]="images/4/right/10.png";
		right_list[10]="images/4/right/11.png";
		right_list[11]="images/4/right/12.png";
		right_list[12]="images/4/right/13.png";
		right_list[13]="images/4/right/14.png";
		right_list[14]="images/4/right/15.png";
		right_list[15]="images/4/right/16.png";
		right_list[16]="images/4/right/17.png";
		right_list[17]="images/4/right/18.png";
		right_list[18]="images/4/right/19.png";
		right_list[19]="images/4/right/20.png";
		right_list[20]="images/4/right/21.png";
		right_list[21]="images/4/right/22.png";
		right_list[22]="images/4/right/23.png";
		right_list[23]="images/4/right/24.png";
		right_list[24]="images/4/right/25.png";
		right_list[25]="images/4/right/26.png";
		right_list[26]="images/4/right/27.png";
		right_list[27]="images/4/right/28.png";
		right_list[28]="images/4/right/29.png";
		right_list[29]="images/4/right/30.png";
		right_list[30]="images/4/right/31.png";
		right_list[31]="images/4/right/32.png";
		right_list[32]="images/4/right/33.png";
		right_list[33]="images/4/right/34.png";
		right_list[34]="images/4/right/35.png";
		right_list[35]="images/4/right/36.png";
		right_list[36]="images/4/right/37.png";
		right_list[37]="images/4/right/38.png";
		right_list[38]="images/4/right/39.png";
		right_list[39]="images/4/right/40.png";
		right_list[40]="images/4/right/41.png";
		right_list[41]="images/4/right/42.png";
		right_list[42]="images/4/right/43.png";
		right_list[43]="images/4/right/44.png";
		right_list[44]="images/4/right/45.png";
		right_list[45]="images/4/right/46.png";
		right_list[46]="images/4/right/47.png";
		right_list[47]="images/4/right/48.png";
		right_list[48]="images/4/right/49.png";
		right_list[49]="images/4/right/50.png";
		right_list[50]="images/4/right/51.png";
		right_list[51]="images/4/right/52.png";
		right_list[52]="images/4/right/53.png";
		right_list[53]="images/4/right/54.png";
		right_list[54]="images/4/right/55.png";
		right_list[55]="images/4/right/56.png";
		right_list[56]="images/4/right/57.png";
		right_list[57]="images/4/right/58.png";
		right_list[58]="images/4/right/59.png";
		right_list[59]="images/4/right/60.png";
		right_list[60]="images/4/right/61.png";
		right_list[61]="images/4/right/62.png";
		right_list[62]="images/4/right/63.png";
		right_list[63]="images/4/right/64.png";
		right_list[64]="images/4/right/65.png";
		right_list[65]="images/4/right/66.png";
		right_list[66]="images/4/right/67.png";
		right_list[67]="images/4/right/68.png";
		right_list[68]="images/4/right/69.png";
		right_list[69]="images/4/right/70.png";
		right_list[70]="images/4/right/71.png";
		right_list[71]="images/4/right/72.png";
		right_list[72]="images/4/right/73.png";
		right_list[73]="images/4/right/74.png";
		right_list[74]="images/4/right/75.png";
		right_list[75]="images/4/right/76.png";
		right_list[76]="images/4/right/77.png";
		right_list[77]="images/4/right/78.png";
		right_list[78]="images/4/right/79.png";
		right_list[79]="images/4/right/80.png";
		right_list[80]="images/4/right/81.png";
		right_list[81]="images/4/right/82.png";
		right_list[82]="images/4/right/83.png";
		right_list[83]="images/4/right/84.png";
		right_list[84]="images/4/right/85.png";
		right_list[85]="images/4/right/86.png";
		right_list[86]="images/4/right/87.png";
		right_list[87]="images/4/right/88.png";
		right_list[88]="images/4/right/89.png";
		right_list[89]="images/4/right/90.png";
		right_list[90]="images/4/right/91.png";
		right_list[91]="images/4/right/92.png";
		right_list[92]="images/4/right/93.png";
		right_list[93]="images/4/right/94.png";
		right_list[94]="images/4/right/95.png";
		right_list[95]="images/4/right/96.png";
		right_list[96]="images/4/right/97.png";
		right_list[97]="images/4/right/98.png";
		right_list[98]="images/4/right/99.png";
		right_list[99]="images/4/right/100.png";
		right_list[100]="images/4/right/101.png";
		right_list[101]="images/4/right/102.png";
		right_list[102]="images/4/right/103.png";
		right_list[103]="images/4/right/104.png";
		right_list[104]="images/4/right/105.png";
		right_list[105]="images/4/right/106.png";
		right_list[106]="images/4/right/107.png";
		right_list[107]="images/4/right/108.png";
		right_list[108]="images/4/right/109.png";
		right_list[109]="images/4/right/110.png";
		right_list[110]="images/4/right/111.png";
		right_list[111]="images/4/right/112.png";
		right_list[112]="images/4/right/113.png";
		right_list[113]="images/4/right/114.png";
		right_list[114]="images/4/right/115.png";
		right_list[115]="images/4/right/116.png";
		right_list[116]="images/4/right/117.png";
		right_list[117]="images/4/right/118.png";
		right_list[118]="images/4/right/119.png";
		right_list[119]="images/4/right/120.png";
		right_list[120]="images/4/right/121.png";
		right_list[121]="images/4/right/122.png";
		right_list[122]="images/4/right/123.png";
		right_list[123]="images/4/right/124.png";
		right_list[124]="images/4/right/125.png";
		right_list[125]="images/4/right/126.png";
		right_list[126]="images/4/right/127.png";
		right_list[127]="images/4/right/128.png";
		right_list[128]="images/4/right/129.png";
		right_list[129]="images/4/right/130.png";
		right_list[130]="images/4/right/131.png";
		right_list[131]="images/4/right/132.png";
		right_list[132]="images/4/right/133.png";
		right_list[133]="images/4/right/134.png";
		right_list[134]="images/4/right/135.png";
		right_list[135]="images/4/right/136.png";
		right_list[136]="images/4/right/137.png";
		right_list[137]="images/4/right/138.png";
		right_list[138]="images/4/right/139.png";
		right_list[139]="images/4/right/140.png";
		right_list[140]="images/4/right/141.png";
		right_list[141]="images/4/right/142.png";
		right_list[142]="images/4/right/143.png";
		right_list[143]="images/4/right/144.png";
		right_list[144]="images/4/right/145.png";
		right_list[145]="images/4/right/146.png";
		right_list[146]="images/4/right/147.png";
		right_list[147]="images/4/right/148.png";
		right_list[148]="images/4/right/149.png";
		right_list[149]="images/4/right/150.png";
		right_list[150]="images/4/right/151.png";
		right_list[151]="images/4/right/152.png";
		right_list[152]="images/4/right/153.png";
		right_list[153]="images/4/right/154.png";
		right_list[154]="images/4/right/155.png";
		right_list[155]="images/4/right/156.png";
		right_list[156]="images/4/right/157.png";
		right_list[157]="images/4/right/158.png";
		right_list[158]="images/4/right/159.png";
		right_list[159]="images/4/right/160.png";
		right_list[160]="images/4/right/161.png";
		right_list[161]="images/4/right/162.png";
		right_list[162]="images/4/right/163.png";
		right_list[163]="images/4/right/164.png";
		right_list[164]="images/4/right/165.png";
		right_list[165]="images/4/right/166.png";
		right_list[166]="images/4/right/167.png";
		right_list[167]="images/4/right/168.png";
		right_list[168]="images/4/right/169.png";
		right_list[169]="images/4/right/170.png";
		right_list[170]="images/4/right/171.png";
		right_list[171]="images/4/right/172.png";
		right_list[172]="images/4/right/173.png";
		right_list[173]="images/4/right/174.png";
		right_list[174]="images/4/right/175.png";
		right_list[175]="images/4/right/176.png";
		right_list[176]="images/4/right/177.png";
		right_list[177]="images/4/right/178.png";
		right_list[178]="images/4/right/179.png";
		right_list[179]="images/4/right/180.png";
		right_list[180]="images/4/right/181.png";
		right_list[181]="images/4/right/182.png";
		right_list[182]="images/4/right/183.png";
		right_list[183]="images/4/right/184.png";
		right_list[184]="images/4/right/185.png";
		right_list[185]="images/4/right/186.png";
		right_list[186]="images/4/right/187.png";
		right_list[187]="images/4/right/188.png";
		right_list[188]="images/4/right/189.png";
		right_list[189]="images/4/right/190.png";
		right_list[190]="images/4/right/191.png";
		right_list[191]="images/4/right/192.png";
		right_list[192]="images/4/right/193.png";
		right_list[193]="images/4/right/194.png";
		right_list[194]="images/4/right/195.png";
		right_list[195]="images/4/right/196.png";
		right_list[196]="images/4/right/197.png";
		right_list[197]="images/4/right/198.png";
		right_list[198]="images/4/right/199.png";
		right_list[199]="images/4/right/200.png";


		
		addMouseListener(this);
		addMouseMotionListener(this);
	  	Dimension size = new Dimension(width, height);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
		setLayout(null);
		
		buttons_pressed[0]=false;
		buttons_pressed[1]=false;
		buttons_pressed[2]=false;
		buttons_pressed[3]=false;
		
	}
	  

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		//draw background
		g.drawImage(imageList[0], 0, 0, null);
		
		//draw menu control

		//home
		if(menu_presseed[0]){
			g.drawImage(imageMenu[1], 1800, 20, null);
		}else{
			g.drawImage(imageMenu[0], 1800, 20, null);
		}
		
		
		//back
		
		if(menu_presseed[1]){
			g.drawImage(imageMenu[5], 20, 950, null);
		}else{
			g.drawImage(imageMenu[4], 20, 950, null);
		}
		
		
		//forward
		if(menu_presseed[2]){
			g.drawImage(imageMenu[3], 1830, 950, null);
		}else{
			g.drawImage(imageMenu[2], 1830, 950, null);
		}
		
		//help
		if(menu_presseed[3]){
			g.drawImage(imageMenu[7], 1732, 20, null);
		}else{
			g.drawImage(imageMenu[6], 1732, 20, null);
		}
		
		//help content
		if(help_pressed){
			g.drawImage(imageMenu[8], 1365, 79, null);
		}
		
		//left table
		g.drawImage(imageList[1], 561, 310, null);
		
		//right table
		g.drawImage(imageList[2], 1035, 310, null);
		
		//left buttons
		if(!buttons_pressed[0]){
			g.drawImage(imageList[3], 910, 560, null);
		}else{
			g.drawImage(imageList[4], 910, 560, null);
		}
		
		if(!buttons_pressed[1]){
			g.drawImage(imageList[5], 910, 640, null);
		}else{
			g.drawImage(imageList[6], 910, 640, null);
		}
		
		//right buttons
		if(!buttons_pressed[2]){
			g.drawImage(imageList[3], 1384, 560, null);
		}else{
			g.drawImage(imageList[4], 1384, 560, null);
		}
		
		if(!buttons_pressed[3]){
			g.drawImage(imageList[5], 1384, 640, null);
		}else{
			g.drawImage(imageList[6], 1384, 640, null);
		}
		
		int position=0;
		for(int i=left_position-2;i<left_position+4;++i){
			if(i<0){
				g.drawImage(new ImageIcon(getClass().getResource(left_list[i+14])).getImage(), 591, 465+position*GAP, null);
			}else if(i>13){
				g.drawImage(new ImageIcon(getClass().getResource(left_list[i-14])).getImage(), 591, 465+position*GAP, null);
			}else{
				g.drawImage(new ImageIcon(getClass().getResource(left_list[i])).getImage(), 591, 465+position*GAP, null);
			}
			++position;
		}
		
		if(left_position<5){
			position=0;
			for(int i=right_position-2;i<right_position+4;++i){
				if(i<0){
					g.drawImage(new ImageIcon(getClass().getResource(right_list[i+200])).getImage(), 591+474, 465+position*GAP, null);
				}else if(i>199){
					g.drawImage(new ImageIcon(getClass().getResource(right_list[i-200])).getImage(), 591+474, 465+position*GAP, null);
				}else{
					g.drawImage(new ImageIcon(getClass().getResource(right_list[i])).getImage(), 591+474, 465+position*GAP, null);
				}
				++position;
			}
		}
		
	}
	
	public void mousePressed(MouseEvent e) {
		
		//home
		if(1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			menu_presseed[0]=true;
		}
		
		//back
		
		if(20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[1]=true;
		}
		
		//forward
		if(1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[2]=true;
		}
		
		//help
		if(1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			menu_presseed[3]=true;
		}
				
		//buttons
		//left
		if(910<=e.getX() && e.getX()<=910+99 
				&& 560<=e.getY() && e.getY()<=560+55){
			buttons_pressed[0]=true;
			buttons_position=0;
		}
		
		if(910<=e.getX() && e.getX()<=910+99 
				&& 640<=e.getY() && e.getY()<=640+55){
			buttons_pressed[1]=true;
			buttons_position=1;
		}
		
		//right
		if(1384<=e.getX() && e.getX()<=1384+99 
				&& 560<=e.getY() && e.getY()<=560+55){
			buttons_pressed[2]=true;
			buttons_position=2;
		}
		
		if(1384<=e.getX() && e.getX()<=1384+99 
				&& 640<=e.getY() && e.getY()<=640+55){
			buttons_pressed[3]=true;
			buttons_position=3;
		}
		
		timer = new Timer(1200, this);
		long_pressed=false;
		timer.start();
		
		this.repaint();
    }

    public void mouseReleased(MouseEvent e) {
    	
    	timer.stop();
    	
    	if(menu_presseed[0] && 1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			//home
    		if(!moving){
    			listener.onHome();
        		moving=true;
    		}
		}else if(menu_presseed[1] && 20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//back
			if(!moving){
				listener.onBack();
				moving=true;
			}
		}else if(menu_presseed[2] && 1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//forward
			if(!moving){
				listener.onChangeValue(4, left_position);
				listener.onChangeValue(5, right_position);
				listener.onForward();
				moving=true;
			}
		}else if(menu_presseed[3] && 1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			help_pressed=!help_pressed;
		}
    	
    	menu_presseed[0]=false;
    	menu_presseed[1]=false;
    	menu_presseed[2]=false;
		menu_presseed[3]=false;
    	
    	//left
		if(910<=e.getX() && e.getX()<=910+99 
				&& 560<=e.getY() && e.getY()<=560+55){
			spinArrow(true,true);
		}
		
		if(910<=e.getX() && e.getX()<=910+99 
				&& 640<=e.getY() && e.getY()<=640+55){
			spinArrow(true,false);
		}
		
		//right
		if(left_position<5){
			if(1384<=e.getX() && e.getX()<=1384+99 
					&& 560<=e.getY() && e.getY()<=560+55){
				spinArrow(false,true);
			}
			
			if(1384<=e.getX() && e.getX()<=1384+99 
					&& 640<=e.getY() && e.getY()<=640+55){
				spinArrow(false,false);
			}
		}
		
    	
    	buttons_pressed[0]=false;
		buttons_pressed[1]=false;
		buttons_pressed[2]=false;
		buttons_pressed[3]=false;
		buttons_position=-1;
		
    	this.repaint();
    }
    
    public void mouseEntered(MouseEvent e) {
       
    }

    public void mouseExited(MouseEvent e) {
       
    }

    public void mouseClicked(MouseEvent e) {
    	
    }
    
    public void mouseMoved(MouseEvent e) {
        
    	
    }
    
    public void mouseDragged(MouseEvent e) {
    	
    }
    
    public void setPanelListener(PanelListener listener){
    	this.listener=listener;
    }
    
    private void spinArrow(boolean left, boolean up){
    	if(left){
    		if(up){
        		--left_position;
        		if(left_position<0){
        			left_position=13;
        		}
        	}else{
        		++left_position;
        		if(left_position>13){
        			left_position=0;
        		}
        	}
    	}else{
    		if(up){
        		--right_position;
        		if(right_position<0){
        			right_position=199;
        		}
        	}else{
        		++right_position;
        		if(right_position>199){
        			right_position=0;
        		}
        	}
    	}
    	
    }
    
    public void actionPerformed(ActionEvent e) {
    	
    	if(!long_pressed){
    		timer.stop();
    		timer = new Timer(100, this);
    		timer.start();
    		long_pressed=true;
    	}else{
    		int x = (int)MouseInfo.getPointerInfo().getLocation().getX();
    		int y = (int)MouseInfo.getPointerInfo().getLocation().getY();
    		
    		if(910<=x && x<=910+99 
    				&& 560<=y && y<=560+55){
    			spinArrow(true,true);
    		}
    		
    		if(910<=x && x<=910+99 
    				&& 640<=y && y<=640+55){
    			spinArrow(true,false);
    		}
    		
    		//right
    		if(left_position<5){
    			if(1384<=x && x<=1384+99 
        				&& 560<=y && y<=560+55){
        			spinArrow(false,true);
        		}
        		
        		if(1384<=x && x<=1384+99 
        				&& 640<=y && y<=640+55){
        			spinArrow(false,false);
        		}
    		}
    		this.repaint();
    	}
    }
    
}